/*
 * 创建日期 2005-10-7
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.view;

import java.util.HashMap;

import org.apache.commons.validator.GenericValidator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.vanda.report.ExcelUtils;
import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;
import com.vanda.report.Validation;
import com.vanda.report.element.BeanElement;

/**
 * 将数据写入指定的Sheet <br>
 * <p>
 * 
 * @author eagle <br>
 * @version Version 1.00 <br>
 */
public class SheetElement extends BeanElement {
	/** 指定生成的sheet名称 */
	private String sheet_name;
	/** */
	private String cloneref;
	private String clonerange;
	
	public void validator() throws ReportException{
		Validation.checkRequired("SheetElement","sheet_name",sheet_name);
	}
	
	/**
	 * 标签执行方法，递归执行
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException{
		ReportInnerData reportData = (ReportInnerData)obj;
		
		try{
			HSSFWorkbook workbook = reportData.getWorkbook();
			HSSFSheet reportSheet = null;
			
			Object contextObj = super.getNameObject(reportData);
			
			reportData.setContextData(contextObj);
			
			
			String name = sheet_name;
			try{
				name = super.getProperty(contextObj,sheet_name).toString();
			}catch(Exception e){
				
			}
			
			//System.out.println("name = "+name);
			if(!GenericValidator.isBlankOrNull(cloneref)){
				int index = workbook.getSheetIndex(cloneref);

				reportSheet = workbook.cloneSheet(index);
				workbook.setSheetName(workbook.getNumberOfSheets()-1,name);
				if(!GenericValidator.isBlankOrNull(clonerange)){
					String[] sPos = clonerange.split(",");
					int start = Integer.parseInt(sPos[0]);
					int end = Integer.parseInt(sPos[1]);
					
					if(start > 1){
						//删除之前的行
						ExcelUtils.removeRow(reportSheet,0,start-2);
					}
					//删除之后的行
					ExcelUtils.removeRow(reportSheet,end,reportSheet.getLastRowNum());
				}
			}else{
				reportSheet = workbook.getSheet(name);
				if(reportSheet == null){
					reportSheet = workbook.createSheet(name);
				}
			}
			
			HashMap hmSheet = (HashMap)reportData.getTemp1();
			hmSheet.put(name,name);
			//ExcelManager.widthAdjust(reportSheet,reportData.getFormatSheet());

			reportData.setReportSheet(reportSheet);
		
			super.execute(obj);
			
		}catch(ReportException e){
			throw e;
		}catch(Exception e){
			throw new ReportException(e);
		}

	}

	/**
	 * @return
	 */
	public String getCloneref() {
		return cloneref;
	}

	/**
	 * @param string
	 */
	public void setCloneref(String string) {
		cloneref = string;
	}



	/**
	 * @return
	 */
	public String getSheet_name() {
		return sheet_name;
	}

	/**
	 * @param string
	 */
	public void setSheet_name(String string) {
		sheet_name = string;
	}
	/**
	 * @return
	 */
	public String getClonerange() {
		return clonerange;
	}

	/**
	 * @param string
	 */
	public void setClonerange(String string) {
		clonerange = string;
	}

}
